<?php
class Muser extends MY_Model{

    public $table = 'user';
    function login($username, $password)
    {
        $password = md5($password);
        $this -> db -> select('user_id, user_name, full_name, password, usergroup_id');
        $this -> db -> from('user');
        $this -> db -> where('user_name', $username);
        $this -> db -> where('password', $password);
        $this -> db -> limit(1);

        $query = $this -> db -> get();
        if($query -> num_rows() == 1)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }

    function getDiscount($uid){
        $query = $this->db->query("select discount from usergroup, user where user.usergroup_id = usergroup.usergroup_id and user.user_id = $uid");
        return $query->result();
    }

    function updateUser($data,$id){
        $this->db->where("user_id",$id);
        if($this->db->update($this->table,$data))
            return TRUE;
        else
            return FALSE;
    }

    function get_user_information($username){
        $this-> db -> select('*');
        $this-> db -> from('user');
        $this-> db -> where('user_name', $username);
        $this-> db -> limit(1);
        $query = $this -> db -> get();
        if($query -> num_rows() == 1)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }

    function check_username($username){
        $this-> db -> select('user_id, user_name, email, full_name');
        $this-> db -> from('user');
        $this-> db -> where('user_name', $username);
        $this-> db -> limit(1);

        $query = $this -> db -> get();
        if($query -> num_rows() == 1)
        {
            return $query->result();
        }
        else
        {
            return false;
        }
    }
    function getUser($perpage,$offset){

        $query = $this->db->get('user',$perpage,$offset);
        return $query->result_array();
    }
    function getUserById($id){
        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('user_id',$id);
        $this->db->limit(1);
        $query = $this->db->get();
        return $query->result_array();
    }
    function countAll(){
        return $this->db->count_all('news');
    }
    function delete($id){
        $this->db->where('user_id',$id);
        $this->db->delete('user');
    }
}